package com.djq.hhc.controller;

import com.djq.hhc.dto.UserInfoDto;
import com.djq.hhc.holder.UserHolder;
import com.djq.hhc.model.post.PostComment;
import com.djq.hhc.model.post.PostInfo;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.PostCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2024/05/03/下午 4:29
 * @Description:
 */

@RestController
@RequestMapping(value = "/post/postComment")
public class PostCommentController {

    @Autowired
    private PostCommentService postCommentService;

    @PostMapping("post")
    public Result saveBlog(@RequestBody PostComment postComment) {
        //获取登录用户
        UserInfoDto user = UserHolder.getUser();
        if (user == null) {
            return Result.fail().message("请先登录");
        }
        //保存评论
        postComment.setUserId(user.getId());
        postComment.setUserName(user.getNickName());
        postCommentService.save(postComment);
        //返回id
        return Result.ok(postComment.getId()).message("发表成功");
    }

    @GetMapping("getCommentsByPostId/{id}")
    public Result getCommentsByPostId(@PathVariable Long id) {
        if (id == null) {
            return Result.fail().message("帖子不存在");
        }

        List<PostComment> list = postCommentService.getCommentsByPostId(id);

        return Result.ok(list);
    }
}
